package com.lanhe.admin.controller.product;

import com.lanhe.common.annotation.Log;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.product.domain.LhLevelInfo;
import com.lanhe.product.service.ILhLevelInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 等级表(LhLevelInfo)表控制层
 *
 * @author zhanggl
 * @since 2024-01-17 15:48:08
 */
@RestController
@RequestMapping("lhLevelInfo")
public class LhLevelInfoController extends BaseController {
	/**
	 * 服务对象
	 */
	@Resource
	private ILhLevelInfoService lhLevelInfoService;

	/**
	 * 分页查询所有数据
	 */
	@PreAuthorize("@ss.hasPermi('product:lhLevelInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(LhLevelInfo lhLevelInfo) {
		startPage();
		List<LhLevelInfo> list = lhLevelInfoService.selectLevelInfoList(lhLevelInfo);
		for (LhLevelInfo levelInfo : list) {
			levelInfo.setPurchaseTypeName(Constants.PurchaseType.getPurchaseTypeName(levelInfo.getPurchaseType()));
		}
		return getDataTable(list);
	}

	/**
	 * 根据编号获取详细信息
	 */
	@PreAuthorize("@ss.hasPermi('product:lhLevelInfo:query')")
	@GetMapping(value = {"/", "/{levelId}"})
	public AjaxResult getInfo(@PathVariable(value = "levelId") Long levelId) {
		return success(lhLevelInfoService.selectLevelInfoById(levelId));
	}

	/**
	 * 新增
	 */
	@PreAuthorize("@ss.hasPermi('product:lhLevelInfo:add')")
	@Transactional
	@Log(title = "会员产品管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody LhLevelInfo levelInfo) {

		levelInfo.setCreateBy(getUsername());
		levelInfo.setCreateTime(new Date());
		return toAjax(lhLevelInfoService.insertLevelInfo(levelInfo));
	}

	/**
	 * 修改
	 */
	@PreAuthorize("@ss.hasPermi('product:lhLevelInfo:edit')")
	@Log(title = "会员产品管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody LhLevelInfo levelInfo) {
		if (ObjectUtils.isEmpty(levelInfo.getLevelId())
				|| null == levelInfo.getLevelId()) {
			return error("主键ID不能为空");
		}
		levelInfo.setUpdateBy(getUsername());
		levelInfo.setUpdateTime(new Date());
		return toAjax(lhLevelInfoService.updateLevelInfo(levelInfo));
	}

	/**
	 * 删除
	 */
	@PreAuthorize("@ss.hasPermi('product:lhLevelInfo:remove')")
	@Log(title = "会员产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{levelIds}")
	public AjaxResult remove(@PathVariable Long[] levelIds) {
		if (ObjectUtils.isEmpty(levelIds)) {
			return error("主键ID不能为空");
		}
		return toAjax(lhLevelInfoService.deleteLevelInfoByIds(levelIds));
	}
}

